<?php
/*****************************************************************************
 * REST_Webservice_Client.php : 
 *****************************************************************************
 * Copyright (C) 2008-2009 The Goalbit Team
 *
 * Authors:    Andres Barrios <andres dot barrios at goalbit-solutions dot com>
 *			   Matias Barrios <matias dot barrios at goalbit-solutions dot com>
 *			   Daniel De Vera <daniel dot de dot vera at goalbit-solutions dot com>
 * 			   Pablo Rodriguez Bocca <pablo dot rodriguez at goalbit-solutions dot com>
 *			   Claudia Rostagnol <claudia dot rostagnol at goalbit-solutions dot com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
?>
<?php

// Error Codes
//
DEFINE( 'STATUS_NO_ERROR'      , 0 );
DEFINE( 'STATUS_GENERAL_ERROR' , 1 );

class REST_Webservice_Client  {


    var $x509cafile   = NULL;
    var $x509certfile = NULL;
    var $x509keyfile  = NULL;


    function set_credencials( $x509cafile, $x509certfile, $x509keyfile )
    {
        $this->x509cafile   = $x509cafile;
        $this->x509certfile = $x509certfile;
        $this->x509keyfile  = $x509keyfile;
    }


    function call_web_method( $webservice_url, $post_args = NULL )
    {
        $session = curl_init($webservice_url);

        curl_setopt( $session, CURLOPT_HEADER, false );
        curl_setopt( $session, CURLOPT_RETURNTRANSFER, true );

        if ( !is_null($post_args) )
        {
            curl_setopt ($session, CURLOPT_POST, true);
            curl_setopt ($session, CURLOPT_POSTFIELDS, json_encode($post_args) );
        }

        if ( !is_null($this->x509certfile) && !is_null($this->x509keyfile) )
        {
            if ( !is_null($this->x509cafile) )
                curl_setopt( $session, CURLOPT_CAINFO, $this->x509cafile );

            curl_setopt( $session, CURLOPT_SSL_VERIFYPEER, true );
            curl_setopt( $session, CURLOPT_SSL_VERIFYHOST, 1 );
            //curl_setopt( $session, CURLOPT_SSL_VERIFYHOST, 2 );
            curl_setopt( $session, CURLOPT_SSLCERT, $this->x509certfile );
            curl_setopt( $session, CURLOPT_SSLKEY, $this->x509keyfile );
        }

        $response = curl_exec($session);

        $curl_error = NULL;
        if ( curl_errno($session) )
            $curl_error = curl_error($session);

        curl_close($session);

        if ( $curl_error !== NULL )
        {
            $response['status']     = STATUS_GENERAL_ERROR;
            $response['status_msg'] = $curl_error;
            return $response;
        }
        elseif ( empty($response) )
        {
            $response['status'] = STATUS_GENERAL_ERROR;
            return $response;
        }

        return json_decode($response, true);
    }


}
?>